import Vue from 'vue'
import VueRouter from 'vue-router'
import PredictionView from '../views/PredictionView.vue'
import LayoutView from "@/views/Layout";
import AboutView from "@/views/AboutView";
import ResultView from "@/views/ResultView";
import IntroduceView from "@/views/IntroduceView"
import LoginView from "@/views/LoginView.vue"
import Intro1View from "@/views/intro1"
import Intro2View from "@/views/intro2"
import RegisterView from "@/views/RegisterView";
import GeneInformationView from "@/views/GeneInformationView";
import HelpView from "@/views/HelpView";
import AboutUsView from "@/views/AboutUsView";
import MyselfView from "@/views/MyselfView";
import GeneScoreView from "@/views/GeneScoreView";

Vue.use(VueRouter)

const routes = [

  {
    path: '/register',
    name: 'register',
    component: RegisterView
  },
  {
    path: '/login',
    name: 'login',
    component: LoginView
  },
  {
    path: '/',
    name: 'Layout',
    component: LayoutView,
    children:[

      {
        path: '',
        name:'home',
        component: IntroduceView
      },
      {
        path:'help',
        name:'help',
        component:HelpView
      },
      {
        path:'aboutUs',
        name:'aboutUs',
        component:AboutUsView
      },
      {
        path: '/myself',
        name: 'myself',
        component: MyselfView
      },
      {
        path: '/intro1',
        name:'intro1',
        component: Intro1View
      },
      {
        path: '/intro2',
        name:'intro2',
        component: Intro2View
      },
      {
        path: '/about',
        name: 'about',
        component:AboutView
      },{
        path: '/prediction',
        name: 'prediction',
        component:PredictionView
      },
      {
        path: '/geneSearch',
        name: 'geneSearch',
        component:GeneInformationView
      },
      {
        path: '/geneScore',
        name: 'geneScore',
        component:GeneScoreView
      },
      {
        path: '/result',
        name: 'result',
        props: true, // 启用路由参数传递
        component:ResultView
      }
    ]
  },


]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})// 路由守卫
router.beforeEach((to, from, next) => {
  if (to.path === '/login') {
    next(); // 如果是登录页面，直接跳转
  } else if (to.path === '/register') {
    next(); // 如果是注册页面，直接跳转
  } else {
    const user = localStorage.getItem("user");
    if (!user) {
      next("/login"); // 如果用户未登录，跳转到登录页面
    } else {
      next(); // 其他情况，继续路由
    }
  }
});

export default router
